﻿Imports Microsoft.Office.Tools.Ribbon
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Diagnostics




Public Class ArasRibbon



    Dim ArasUrl As String
    Dim ArasDB As String
    Dim ArasUser As String
    Dim ArasPass As String
    Dim UserLocalDir As String

    Dim ActiveDoc As Microsoft.Office.Interop.Word.Document



    Private Sub NewDoc_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles NewDoc.Click

        'Ouverture d'une fenetre de création de document pour meta-données avant création du word




    End Sub

    Private Sub Open_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Open.Click

        'ouverture d'aras innovator avec la liste des documents disponibles

    End Sub



    Private Sub Lock_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Lock.Click

        ' lock du document, desactive le bouton lock et active le bouton unlock
        Dim result As String
        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
        result = ConnecAras.lockDoc(ItemNumber.Label)

        If result <> "error" Then
            MsgBox("Locked with id: " + result)
            ItemNumber.Label = result

            UnLock.Enabled = True
            Save.Enabled = True
            Lock.Enabled = False
        Else
            MsgBox("Error while locking Id:" + ItemNumber.Label)
        End If



    End Sub




    Private Sub CreateFromActual_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles CreateFromActual.Click

        'Create a document with actual file attached + invite de commande pour méta données


    End Sub




    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Save.Click

        Dim result As String
        'uploader la nouvelle version du fichier
        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
        Dim FilepathComplete As String = UserLocalDir + "\" + ActiveDoc.Variables("DocName").Value + ".docx"

        ActiveDoc.SaveAs(CStr(FilepathComplete))

        'Check if document has already a file attached
        Dim hasfile As Boolean
        hasfile = ConnecAras.DocHasFileTest(ItemNumber.Label)
        If hasfile = True Then
            result = ConnecAras.UploadUpdatedDoc(ItemNumber.Label, FilepathComplete, ActiveDoc.Variables("DocName").Value + ".docx")
        Else
            result = ConnecAras.UploadNewDoc(ItemNumber.Label, FilepathComplete, ActiveDoc.Variables("DocName").Value + ".docx")
        End If
        ItemNumber.Label = result
        MsgBox("saved with id: " + result)


    End Sub

    Private Sub UnLock_Click_1(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles UnLock.Click
        UnlockDoc()

    End Sub

    Private Sub UnlockDoc()

        'verifier si le document est sauvegardé
        Dim DocIsSaved As Boolean = False
        Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
        Dim result As String
        Dim NewItemID As String
        NewItemID = ItemNumber.Label

        'verifier si le document a déjà un fichier
        Dim DocHasFile As Boolean
        If DocIsSaved = True Then
            DocHasFile = True
        Else
            DocHasFile = ConnecAras.DocHasFileTest(ItemNumber.Label)
        End If

        'si non sauvegardé proposition de sauvegarder
        If (DocIsSaved = False) Then
            Dim QuestionResult As Integer
            QuestionResult = MsgBox("Do you want to Save this version before unlocking the document?", MsgBoxStyle.YesNoCancel, "confirm saving the doc")
            If QuestionResult = DialogResult.Yes Then
                ' save + upload + unlock
                Dim FilepathComplete As String = UserLocalDir + "\" + ActiveDoc.Variables("DocName").Value + ".docx"
                ActiveDoc.SaveAs(CStr(FilepathComplete))
                MsgBox(FilepathComplete)
                If DocHasFile = True Then
                    NewItemID = ConnecAras.UploadUpdatedDoc(ItemNumber.Label, FilepathComplete, ActiveDoc.Variables("DocName").Value + ".docx")
                    NewItemID = ConnecAras.UnlockDoc(NewItemID)
                Else
                    NewItemID = ConnecAras.UploadNewDoc(ItemNumber.Label, FilepathComplete, ActiveDoc.Variables("DocName").Value + ".docx")
                    NewItemID = ConnecAras.UnlockDoc(ItemNumber.Label)
                End If
                'MsgBox(result)
                ItemNumber.Label = NewItemID
                MsgBox("saved and Unlocked with id: " + NewItemID)
                Lock.Enabled = True
                Save.Enabled = False
                UnLock.Enabled = False

            ElseIf QuestionResult = DialogResult.No Then
                result = ConnecAras.UnlockDoc(ItemNumber.Label)
                MsgBox("saved and Unlocked with id: " + NewItemID)
                Lock.Enabled = True
                Save.Enabled = False
                UnLock.Enabled = False
                ' unlock
            Else
                ' cancel

            End If

        End If

    End Sub

    Private Sub NewDoc_Disposed(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub TabAras_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabAras.Disposed

    End Sub


    Private Sub CreateINI_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles CreateINI.Click
        Dim FormulaireLogin As New LoginForm1
        FormulaireLogin.Show()

    End Sub

    Private Sub ArasRibbon_Load(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonUIEventArgs) Handles Me.Load
        Basic.Visible = False
        State.Visible = False




    End Sub

    Private Sub GetInfo_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles GetInfo.Click
        'init Prodeos

        Dim myBuildInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo(Application.ExecutablePath)
        Version.Label = myBuildInfo.ProductBuildPart


        Dim GetParam As New ComINI
        Dim username As String
        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
        username = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "User")
        If username = "" Then
            username = "user not set"
            User.Text = username
        Else
            User.Text = username
            Get_Doc_Informations()
            Basic.Visible = True
            State.Visible = True

        End If

    End Sub

    Public Sub Get_Doc_Informations()


        Dim GetParam As New ComINI
        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
        ActiveDoc = Globals.ThisAddIn.Application.ActiveDocument
        'MsgBox(ActiveDoc.Variables.Count)

        If (ActiveDoc.Variables.Count > 0) Then
            ItemNumber.Label = ActiveDoc.Variables("ItemNumber").Value
            DocName.Label = "Name :" + ActiveDoc.Variables("DocName").Value
            DocNum.Label = "Doc Number :" + ActiveDoc.Variables("DocNum").Value
            Try
                DocClass.Label = "Class :" + ActiveDoc.Variables("DocClass").Value
            Catch ex As Exception
                DocClass.Label = "Class :" + "No Class"
            End Try


            ArasUrl = ActiveDoc.Variables("ArasUrl").Value

            ArasDB = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "DB")
            ArasUser = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "User")
            ArasPass = GetParam.GetCle(folder + "\InnovAras.ini", "Connection_Settings", "Pass")


            UserLocalDir = ActiveDoc.Variables("UserLocalDir").Value


            'test if doc is locked
            Dim DocIsLocked As Integer
            Dim ConnecAras As New ConnecteurAras(ArasUrl, ArasDB, ArasUser, ArasPass)
            DocIsLocked = ConnecAras.IsDocLocked(ItemNumber.Label)
            If DocIsLocked = 1 Then
                Save.Enabled = True
                Lock.Enabled = False
                UnLock.Enabled = True
                CloseDoc.Enabled = True
                CreateFromActual.Enabled = False
            ElseIf DocIsLocked = 2 Then
                Save.Enabled = False
                Lock.Enabled = False
                UnLock.Enabled = False
                CloseDoc.Enabled = True
                CreateFromActual.Enabled = False
                MsgBox("This Doc is locked by someone else")
            Else
                Save.Enabled = False
                Lock.Enabled = True
                UnLock.Enabled = False
                CloseDoc.Enabled = True
                CreateFromActual.Enabled = False
            End If
            MsgBox("Local Dir: " + UserLocalDir + vbCrLf + "ID: " + ItemNumber.Label)


        Else
            MsgBox("This Doc is not made from Aras")
        End If


    End Sub

    Private Sub ArasRibbon_LoadImage(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonLoadImageEventArgs) Handles Me.LoadImage

    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Private Sub CloseDoc_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles CloseDoc.Click
        If Lock.Enabled = False Then
            Dim QuestionResult As Integer
            QuestionResult = MsgBox("Do you want to Unlock the document before closing?", MsgBoxStyle.YesNoCancel, "confirm unlock the doc")
            If QuestionResult = DialogResult.Yes Then
                UnlockDoc()
                Application.Exit()
            ElseIf QuestionResult = DialogResult.No Then

                Application.Exit()
            Else

            End If
        Else
            Application.Exit()

        End If
    End Sub
End Class
